package cn.com.xuxiaowei.server.intercptor;

import lombok.SneakyThrows;
import org.apache.cxf.binding.soap.SoapMessage;
import org.apache.cxf.interceptor.Fault;
import org.apache.cxf.phase.AbstractPhaseInterceptor;
import org.apache.cxf.phase.Phase;

import javax.xml.XMLConstants;
import javax.xml.namespace.QName;
import javax.xml.soap.SOAPEnvelope;
import javax.xml.soap.SOAPHeader;
import javax.xml.soap.SOAPHeaderElement;
import javax.xml.soap.SOAPMessage;

/**
 * @Description 客户端登录拦截器
 * @Author weisl
 * @Date 2024-04-11 08:53
 */
public class ClientLoginInterceptor extends AbstractPhaseInterceptor<SoapMessage> {
    private String username;
    private String password;

    public ClientLoginInterceptor() {
        super(Phase.PREPARE_SEND);
    }

    public ClientLoginInterceptor(String username, String password) {
        super(Phase.PREPARE_SEND);
        this.username = username;
        this.password = password;
    }

    @SneakyThrows
    @Override
    public void handleMessage(SoapMessage message) throws Fault {

        SOAPMessage soapMessage = message.getContent(SOAPMessage.class);
        // 获取 SOAP 头部
        SOAPHeader header = soapMessage.getSOAPHeader();
        SOAPEnvelope envelope = soapMessage.getSOAPPart().getEnvelope();
        // 如果头部尚未创建，需要先创建它
        if (header == null) {
            header = envelope.addHeader();
        }
        envelope.addNamespaceDeclaration("gat", "http://schemas.datacontract.org/2004/07/Gateway.Servicios");
        QName passwordQname = header.createQName("Password", "gat");
        QName userQname = header.createQName("UserName", "gat");
        // Here, I’m trying to add a QName with no namespace.
        QName qNameUserCredentials = new QName(XMLConstants.NULL_NS_URI, "MyHeader");
        SOAPHeaderElement userCredentials = header.addHeaderElement(qNameUserCredentials);
        SOAPHeaderElement password = header.addHeaderElement(passwordQname);
        password.addTextNode(this.password);
        SOAPHeaderElement username = header.addHeaderElement(userQname);
        username.addTextNode(this.username);
        userCredentials.addChildElement(password);
        userCredentials.addChildElement(username);
        soapMessage.saveChanges();

        // List<Header> headers = soapMessage.getHeaders();
        // Document doc = DOMUtils.createDocument();
        // Element auth = doc.createElement("wsse:Security");
        // Element username = doc.createElement("username");
        // Element password = doc.createElement("password");
        // username.setTextContent(this.username);
        // password.setTextContent(this.password);
        // auth.appendChild(username);
        // auth.appendChild(password);
        // headers.add(0, new Header(new QName(XMLConstants.NULL_NS_URI, "Header"), auth));
    }

    // public boolean handleMessage(SOAPMessageContext context) {
    //     try {
    //         SOAPMessage message = context.getMessage();
    //         SOAPHeader header = message.getSOAPHeader();
    //         SOAPEnvelope envelope = message.getSOAPPart().getEnvelope();
    //         if (header == null) {
    //             header = envelope.addHeader();
    //         }
    //         envelope.addNamespaceDeclaration("gat", "http://schemas.datacontract.org/2004/07/Gateway.Servicios");
    //         QName passwordQname = header.createQName("Password", "gat");
    //         QName userQname = header.createQName("User", "gat");
    //         // Here, I’m trying to add a QName with no namespace.
    //         QName qNameUserCredentials = new QName(XMLConstants.NULL_NS_URI, "MyHeader");
    //         SOAPHeaderElement userCredentials = header.addHeaderElement(qNameUserCredentials);
    //         SOAPHeaderElement password = header.addHeaderElement(passwordQname);
    //         password.addTextNode(this.password);
    //         SOAPHeaderElement username = header.addHeaderElement(userQname);
    //         username.addTextNode(this.username);
    //         userCredentials.addChildElement(password);
    //         userCredentials.addChildElement(username);
    //         message.saveChanges();
    //         StringWriter writer = new StringWriter();
    //         message.writeTo(new StringOutputStream(writer));
    //         System.out.println("SOAP message: \n" + writer.toString());
    //     } catch (SOAPException e) {
    //         System.out.println("Error occurred while adding credentials to SOAP header." + e.getMessage());
    //     } catch (IOException e) {
    //         System.out.println("Error occurred while writing message to output stream." + e.getMessage());
    //     }
    //     return true;
    // }
}
